﻿/********************************************************************************/
// <copyright file="EventService.cs" company="Asia E-Business Solutions">
//     Copyright © 2012. All right reserved
// </copyright>
// <history>
//     <change who="Phuoc Le" date="31/12/2012 1:19:14 PM">Created</change>
// </history>
/********************************************************************************/

using System;
using System.Collections.Generic;
using JLL.China.Model;
using JLL.China.Infrastructure.RepositoryFramework;
using JLL.China.Infrastructure.Repositories;
using JLL.China.Infrastructure.Repositories.Event;
using JLL.China.Model.DTO;

namespace JLL.China.Infrastructure.Services
{
    public static class EventService
    {
        private static IEventRepository repository;

        static EventService()
        {
            repository = RepositoryFactory.GetRepository<IEventRepository, Model.Event, int>(new LINQ2SQLUnitOfWork<int>()); 
        }

        public static IList<Event> GetAllEvents()
        {
            return repository.FindAll();
        }

        public static Event GetEvent(decimal? id)
        {
            return repository.FindById(id);
        }

        public static void CreateEvent(Event evt)
        {
            repository.Add(evt);
        }

        public static decimal CreateNewEvent(Event evt,Event evt_local)
        {
            evt_local.DateUpdated = DateTime.Now;
            evt_local.EventDate = evt.EventDate;
            evt_local.EventDateEnd = evt.EventDateEnd;
            evt_local.EventTime = evt.EventTime;
            evt_local.EventTimeEnd = evt.EventTimeEnd;
            evt_local.EventColor = evt.EventColor;
            evt_local.Status = evt.Status;
            evt_local.ContactEmail = evt.ContactEmail;
            evt_local.Admin = evt.Admin;
            evt_local.EventType = evt.EventType;
            evt_local.DateUpdated = evt.DateUpdated;
            evt_local.CreatedBy = evt.CreatedBy;
            evt_local.DateCreated = evt.DateCreated;

            evt.LanguageFID = 1;
            decimal id =  repository.CreateNewEvent(evt);
            evt_local.Event_ID = id;
            evt_local.LanguageFID = 2;
            return repository.CreateNewEvent(evt_local);
        }

        public static void UpdateEvent(Model.Event evt,Model.Event evt_local)
        {
            evt_local.DateUpdated = DateTime.Now;
            evt_local.EventDate = evt.EventDate;
            evt_local.EventDateEnd = evt.EventDateEnd;
            evt_local.EventTime = evt.EventTime;
            evt_local.EventTimeEnd = evt.EventTimeEnd;
            evt_local.EventColor = evt.EventColor;
            evt_local.Status = evt.Status;
            evt_local.ContactEmail = evt.ContactEmail;
            evt_local.Admin = evt.Admin;
            evt_local.EventType = evt.EventType;
            evt_local.DateUpdated = evt.DateUpdated;
            evt_local.CreatedBy = evt.CreatedBy;
            evt_local.DateCreated = evt.DateCreated;
            repository.UpdateEvent(evt);
            CommitChanges();
            repository.UpdateEvent(evt_local);
            CommitChanges();
        }
        public static void UpdateEvent(Model.Event evt)
        {
            repository.UpdateEvent(evt);
        }
        public static void DeleteEvent(decimal event_ID)
        {
            repository.DeleteEvent(event_ID);
        }

        public static IList<Model.Event> Search(EventSearchCriteria searchCriteria)
        {
            return repository.Search(searchCriteria);
        }

        public static void CommitChanges()
        {
            repository.UnitOfWork.Commit();
        }

        public static void ChangeEventStatus(string arrayID,bool? status)
        {
            repository.ChangeStatus(arrayID,status);
        }

        public static void RemoveSelected(string arrayID)
        {
            repository.RemoveSelected(arrayID);
        }
        public static Model.Event FindByIDandLanguageFID(decimal? eventId, decimal languageFid)
        {
            return repository.FindByIDandLanguageFid(eventId, languageFid);
        }

    }
}
